﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace KinectAdapter
{
    public class KinectSlider : INotifyPropertyChanged
    {        
        private double val = 0.0;
        private double realVal = 0.0;
        private double scale = 1.0;

        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Resets the slider back to its initial value.
        /// </summary>
        public void Reset()
        {
            val = 0.0;
            realVal = 0.0;
            NotifyPropertyChanged("Value");
        }

        /// <summary>
        /// Gets or sets the value of the slider.
        /// Doesn't snap to zero but scales.
        /// </summary>
        public double Value {
            get { return val; }
            set
            {
                double clamped = Clamp(value*scale);
                if (val == clamped) return;

                val = clamped;
                realVal = val;
                NotifyPropertyChanged("Value");
            }
        }

        public double Max { get; set; }

        public double Min { get; set; }

        /// <summary>
        /// Values set on Value property and added or substracted from it
        /// by ChangeValue will be multiplied by Scale.
        /// </summary>
        public double Scale
        {
            get { return scale; }
            set { scale = value; }
        }

        /// <summary>
        /// Range of values around 0 that will be interpreted as 0.
        /// If Min is -1, Max is 1 and ZeroRange is 0.1 then
        /// values from -0.1 to 0.1 will be interpreted as 0.
        /// </summary>
        public double ZeroRange { get; set; }

        /// <summary>
        /// Changes the value of the slider by a given value.
        /// The value is scaled first.
        /// </summary>
        /// <param name="change"></param>
        public void ChangeValue(double change)
        {
            if (change == 0.0) return;  // no change
            
            double lastVal = val;

            realVal = realVal + change * scale;
            realVal = Clamp(realVal);

            val = realVal;

            if (lastVal != val)
                NotifyPropertyChanged("Value");
        }

        /// <summary>
        /// Changes the value of the slider by a given value.
        /// The value is scaled first. By using this method, not the Value property,
        /// to change the value, it will be properly snapped to zero when required. 
        /// </summary>
        /// <param name="change"></param>
        public void ChangeValueSnapToZero(double change)
        {
            if (change == 0.0) return;  // no change

            double lastVal = val;

            realVal = realVal + change * scale;
            realVal = Clamp(realVal);

            if (realVal > ZeroRange || realVal < ZeroRange * -1)    // outside of zero range              
                val = realVal;
            else // snapping to zero
                val = 0;

            if (lastVal != val)
                NotifyPropertyChanged("Value");
        }


        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        /// <summary>
        /// Returns the clamped to the desired value range.
        /// </summary>
        /// <param name="val">Value to be clamped.</param>
        /// <returns>Clamped value.</returns>
        private double Clamp(double val)
        {
            return val < Min ? Min : val > Max ? Max : val;
        }

    }
}
